//
//  EventParser.m
//  FootPrint
//
//  Created by alex tao on 3/21/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "EventParser.h"
#import "EventQueryByIdRequest.pb.h"
#import "EventBoardQueryRequest.pb.h"
#import "EventBoardRequest.pb.h"
#import "EventModifyRequest.pb.h"
#import "EventMemberModifyRequest.pb.h"
#import "EventCreateRequest.pb.h"
#import "EventCancelRequest.pb.h"

@implementation EventParser

- (void)dealloc
{
    _delegate = nil;
    [super dealloc];
}

- (void) updateEventWithId:(long long)eventId
{
    NSData * queryData = [[[[EventQueryByIdRequestMessage builder] setEventId:eventId] build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_EVENT_BY_ID postData:queryData completion:^(NSData * resultData) {
            @try {
                EventQueryByIdResponseMessage * resp = [EventQueryByIdResponseMessage parseFromData:resultData];
                [_delegate didRecieveEventInfo:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"暂时无法获得参与用户信息!"] setDuration:2000] show];
                [_delegate didRecieveEventInfo:nil];
            }
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didRecieveEventInfo:nil];
        }];
    }
}

- (void) updateMsgWithId:(long long)eventId
{
    NSData * msgData = [[[[EventBoardQueryRequestMessage builder] setEventId:eventId] build] data];
    if (msgData) {
        [super noCacheRequestURL:URL_QUARY_EVENT_BOARD postData:msgData completion:^(NSData * resultData) {
            @try {
                EventBoardQueryResponseMessage * resp = [EventBoardQueryResponseMessage parseFromData:resultData];
                [_delegate didRecieveEventMsg:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"暂时无法获得留言信息!"] setDuration:2000] show];
                [_delegate didRecieveEventMsg:nil];
            }
            
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didRecieveEventMsg:nil];
        }];
    }
}

- (void) addEventBoardMsg:(NSString*)msg forEventId:(long long)eventId andUserId:(long long)uid
{
    NSData * msgData = [[[[[[EventBoardRequestMessage builder] setEventId:eventId] setUserId:uid] setContent:msg] build] data];
    if (msgData) {
        [super noCacheRequestURL:URL_EVENT_BOARD postData:msgData completion:^(NSData * resultData) {
            @try {
                EventBoardResponseMessage * resp = [EventBoardResponseMessage parseFromData:resultData];
                [_delegate didAddEventMsg:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"暂时无法留言信息!"] setDuration:2000] show];
                [_delegate didAddEventMsg:nil];
            }
            
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didAddEventMsg:nil];
        }];
    }
}

- (void) modifyEvent:(EventMessage*)msg forUser:(long long)uid
{
    NSData * msgData = [[[[[EventModifyRequestMessage builder] setUserId:uid] setEvent:msg] build] data];
    if (msgData) {
        [super noCacheRequestURL:URL_MODIFY_EVENT postData:msgData completion:^(NSData * resultData) {
            @try {
                EventModifyResponseMessage * resp = [EventModifyResponseMessage parseFromData:resultData];
                [_delegate didModifyEvent:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"修改事件失败!"] setDuration:2000] show];
                [_delegate didModifyEvent:nil];
            }
            
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didModifyEvent:nil];
        }];
    }
}

- (void) modifyEvent:(long long)eventId andUserId:(long long)uid addMember:(NSArray*)add removeMember:(NSArray*)sub
{
    if (add.count == 0 && sub.count == 0) {
        [_delegate didModifyEventMember:nil];
        return;
    }
    EventMemberModifyRequestMessage_Builder * builder = [[[EventMemberModifyRequestMessage builder] setUserId:uid] setEventId:eventId];
    if (add.count > 0) {
        [builder setAddMemberIdArray:add];
    }
    if (sub.count > 0) {
        [builder setRemoveMemberIdArray:sub];
    }
    
    NSData * msgData = [[builder build] data];
    if (msgData) {
        [super noCacheRequestURL:URL_EVENT_MENBER_MODIFY postData:msgData completion:^(NSData * resultData) {
            @try {
                EventMemberModifyResponseMessage * resp = [EventMemberModifyResponseMessage parseFromData:resultData];
                [_delegate didModifyEventMember:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"修改成员失败!"] setDuration:2000] show];
                [_delegate didModifyEventMember:nil];
            }
            
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didModifyEventMember:nil];
        }];
    }
}

- (void) createEvent:(EventMessage*)msg forUser:(long long)uid
{
    if (nil == msg || uid <= 0) {
        [_delegate didCreateEvent:nil];
        return;
    }
    EventCreateRequestMessage_Builder * builder = [[[EventCreateRequestMessage builder] setUserId:uid] setEvent:msg];

    NSData * msgData = [[builder build] data];
    if (msgData) {
        [super noCacheRequestURL:URL_CREATE_EVENT postData:msgData completion:^(NSData * resultData) {
            @try {
                EventCreateResponseMessage * resp = [EventCreateResponseMessage parseFromData:resultData];
                [_delegate didCreateEvent:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"创建事件失败!"] setDuration:2000] show];
                [_delegate didCreateEvent:nil];
            }
            
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didCreateEvent:nil];
        }];
    }
}

- (void) cancelEvent:(long long)eventId forUser:(long long)uid
{
    if (eventId <= 0 || uid <= 0) {
        [_delegate didCancleEvent:nil];
        return;
    }
    EventCancelRequestMessage_Builder * builder = [[[EventCancelRequestMessage builder] setUserId:uid] setEventId:eventId];
    
    NSData * msgData = [[builder build] data];
    if (msgData) {
        [super noCacheRequestURL:URL_CANCEL_EVENT postData:msgData completion:^(NSData * resultData) {
            @try {
                EventCancelResponseMessage * resp = [EventCancelResponseMessage parseFromData:resultData];
                [_delegate didCancleEvent:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"删除事件失败!"] setDuration:2000] show];
                [_delegate didCancleEvent:nil];
            }
            
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didCancleEvent:nil];
        }];
    }
}

@end
